﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Work.BL.Managers;
using Work.BL.POCOs;
using Work.UI.Web.MVC.Code.Common;
using Work.UI.Web.MVC.Models.Vacancy;

namespace Work.UI.Web.MVC.Controllers
{
    public class VacancyController : Controller
    {
        #region Fields

        private const string EMPLOUYER = "Employer";
        private readonly ISectionManager _sectionManager;
        private readonly IRegionManager _regionManager;
        private readonly IVacancyManager _vacancyManager;

        #endregion

        #region Constructors

        public VacancyController(ISectionManager sectionManager, IRegionManager regionManager, IVacancyManager vacancyManager)
        {
            _sectionManager = sectionManager;
            _regionManager = regionManager;
            _vacancyManager = vacancyManager;
        }

        #endregion

        #region WebMethods

        [HttpGet]
        public ActionResult AddVacancy()
        {
            ViewBag.Section = _sectionManager.GetSections();
            ViewBag.Region = _regionManager.GetRegions();
            return View(new VacancyModel());
        }

        [HttpPost]
        public ActionResult AddVacancy(VacancyModel vacancyModel)
        {
            if(ModelState.IsValid)
            {
                AddVacancyHelper(vacancyModel);
            }

            ViewBag.Section = _sectionManager.GetSections();
            ViewBag.Region = _regionManager.GetRegions();
            return View(new VacancyModel());
        }

        #endregion

        #region Helpers

        private void AddVacancyHelper(VacancyModel model)
        {
            VacancyPOCO poco = new VacancyPOCO();
            poco.Date = DateTime.Now;
            poco.Description = model.Description;
            poco.IdRegion = model.IdRegion;
            poco.IdSection = model.IdSection;
            poco.IsVisible = model.IsVisible;
            poco.Post = model.Post;
            poco.Requirements = model.Requirements;
            poco.Salary = model.Salary;
            poco.Title = model.Title;

            _vacancyManager.Insert(poco);
        }

        #endregion

    }
}
